import { ElMessageBox } from "element-plus";
import { HandleData } from "./interface";
import i18n from "@/languages/index";
const { t } = i18n.global;

/**
 * @description 操作单条数据信息(二次确认【删除、禁用、启用、重置密码】)
 * @param {Function} api 操作数据接口的api方法(必传)
 * @param {Object} params 携带的操作数据参数 {id,params}(必传)
 * @param {String} message 提示信息(必传)
 * @param {String} confirmType icon类型(不必传,默认为 warning)
 * @return Promise
 */
export const useHandleData = <P = any, R = any>(
	api: (params: P) => Promise<R>,
	params: Parameters<typeof api>[0] | any,
	message: string | undefined,
	confirmType: HandleData.MessageType = "warning"
) => {
	return new Promise<R>(resolve => {
		ElMessageBox.confirm(`${t("useaddress.elmsg.title")}${message}?`, t("useaddress.elmsg.tips"), {
			confirmButtonText: t("useaddress.elmsg.confirm"),
			cancelButtonText: t("useaddress.elmsg.cancel"),
			type: confirmType,
			draggable: true
		}).then(async () => {
			const res = await api(params);
			resolve(res);
		});
	});
};
